Getting Started: My First Cookbook!
Objective
This guide will help you create a new cookbook from scratch, and converge the code on a local VM using TestKitchen and ChefDK components.
The final product will be a Jenkins Master instance running in a VM on your laptop. This cookbook then be used to deploy to any target environment running Chef, including Production!
Prerequisites
Enable VT-X (in BIOS) Install Git For installation on Windows
Add Git path - 'setx PATH "%PATH%;C:\Program Files(x86)\Git\bin" /m' or path where Git installed Set HTTP_PROXY - 'setx HTTP_PROXY http://proxy-src.research.ge.com:8080 /m' Set HTTPS_PROXY - 'setx HTTPS_PROXY http://proxy-src.research.ge.com:8080 /m' To manage line delimiter correctly, configure git and IDE such as Aptana/Windows according to Notes below Create a new working directory for Chef Cookbooks
This can be whatever you like, I use ~/Documents/code/cookbooks
Change to your cookbooks directory cd ~/Documents/code/cookbooks
Create a new cookbook skeleton using Berkshelf
Execute: berks cookbook pipeline
This will create an initialized skeletal directory and file structure for a new cookbook called 'pipeline'.
cd pipeline
If you look around in this directory you will see some of the core files and directories needed to create a cookbook Of note are the .kitchen.yml and the recipes and attributes directories Note for Windows install
I had to manually add Vagrant path C:\HashiCorp\Vagrant\bin even though Vagrant is supposed to be run from Kitchen I had to downgrade Virtualbox to 4.3.12 since version 4.3.16 (and the latest 4.3.20) have issues to start any VMs after a Windows update released on 12/11/14. Add a jenkins cookbook dependency to metadata.rb
Add the following line to the end of the metadata.rb file
depends 'jenkins'
What is the metadata.rb file?
The metadata.rb file contains metadata about your cookbook, most importantly: author/maintainer version dependencies Use Berkshelf to resolve and download dependency cookbooks
Execute: berks install
This will look in both the Berksfile config file for directives (like where it should look to resolve dependencies), as well as if it should look in the metadata file for additional dependency declarations. By default it will also look in metadata.rb
If berks install executed without error, you should now see a jenkins cookbook in your local berkshelf cache (typically in ~/.berkshelf/cookbooks/)
Feel free to explore and compare the Jenkins cookbook with your 'pipeline cookbook'. You should see a recipes directory with Jenkins recipes in it.
Windows Users: If you get an error around SSL Verification You may need to set SSL_CERT Variable to where ever you installed chef
set SSL_CERT_FILE=C:\opscode\chefdk\embedded\ssl\certs\cacert.pem
Add jenkins to recipes/default.rb to create a jenkins master, as a prerequisite, we'll also need to install java
Using your favorite editor add the following 2 lines to the end of the recipes/default.rb file.
include_recipe 'jenkins::java' include_recipe 'jenkins::master' Modify .kitchen.yml to add default recipe and attribute
suites:
- name: default
run_list: pipeline::default
attributes:
jenkins:
Modify .kitchen.yml to add cpu/memory and port forwarding for jenkinsmaster: install_method: package
driver_config: network:
- ["forwarded_port", {guest: 8080, host: 8080}] customize: memory: 2048 cpus: 2 If you are working behind GE Proxies or on GE VPN, you will need to specify proxy settings in a .kitchen.local.yml
driver: name: vagrant http_proxy: http://proxy-src.research.ge.com:8080 https_proxy: http://proxy-src.research.ge.com:8080 no_proxy: "10., 127.0.0.1, localhost, 3., *.ge.com"
provisioner: name: chef_zero client_rb: http_proxy: http://proxy-src.research.ge.com:8080 https_proxy: http://proxy-src.research.ge.com:8080 no_proxy: "10., 127.0.0.1, localhost, 3., *.ge.com" Show all kitchen instances
Execute: kitchen list
Instance Driver Provisioner Last Action
jenkins-centos-64 Vagrant ChefZero
Execute: kitchen converge centos
This is where the magic begins, a quick outline of what will happen: 1. TestKitchen will call Vagrant to start up a CentOS 6.4 image, if this is the first time you are requesting a VM with this OS type, Vagrant will locate a base, generic image from the internet, and download it to your local laptop. This can take a few minutes, and only happens once per OS type. 1. Once the machine image is downloaded, Vagrant will start the VM given the Network, Memory, and CPU parameters defined in the .kitchen.yml file 1. Once the VM has booted, TestKitchen will SSH into the machine and attempt to download the chef-client from the internet. 1. Once the chef-client is installed, TestKitchen will inject all of the cookbooks created or downloaded using berkshelf 1. Lastly it will attempt to converge the node, running the recipes defined in the nodes run-list.
If all goes well you should now have a fully functional Jenkins server
See if jenkins is running
In your favorite browser navigate to: http://localhost:8080
Notes - How to set line delimiter in Aptana/Windows
Windows uses LFCR for line delimiter while Mac/UNIX uses LF. The CR character introduced in Windows text files might cause unexpected errors while consumed in UNIX. Following procedure explains how line delimiter is converted to UNIX.
To change how Git handles line delimiter - set AutoCRLF to false
git config --global core.autocrlf false To change how Aptana handles line delimiter - set Text editor preference
Go to tab Window->Preference
Under General->Workspace, set New text file line delimiter to Other and select Unix
To convert line delimiter to Unix for existing files, use tab File->Convert Line Delimiters to->Unix